package com.jstyle.beatecg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.beatecg.R;
import com.jstyle.beatecg.model.HrvData;
import com.jstyle.beatecg.util.LanguageHelp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/3/5.
 */

public class EcgHistoryAdapter extends BaseAdapter {
    LayoutInflater layoutInflater;
    List<HrvData> fileList;
    EcgHistoryListener ecgHistoryListener;
    public EcgHistoryAdapter(Context context, List<HrvData> fileListv, EcgHistoryListener ecgHistoryListener) {
        this.layoutInflater=LayoutInflater.from(context);
        this.ecgHistoryListener=ecgHistoryListener;
        this.fileList=fileListv;


    }

    @Override
    public int getCount() {
        return fileList==null?0:fileList.size();
    }

    @Override
    public Object getItem(int position) {
        return fileList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        EcgViewHolder viewHolder=null;

        if(convertView==null){
            convertView=layoutInflater.inflate(R.layout.item_ecg_history,null);
            viewHolder = new EcgViewHolder();
            viewHolder.tv_FileName= (Button) convertView.findViewById(R.id.tv_ecg_fileName);
            viewHolder.bt_watch = (Button) convertView.findViewById(R.id.bt_ecg_history_watch);
            viewHolder.bt_history = (Button) convertView.findViewById(R.id.bt_ecg_history_history);
            viewHolder.replay = (Button) convertView.findViewById(R.id.replay);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (EcgViewHolder) convertView.getTag();
        }
       viewHolder.bt_watch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ecgHistoryListener!=null)ecgHistoryListener.onHistoryClick(fileList.get(position));
            }
        });
        viewHolder.bt_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ecgHistoryListener!=null)ecgHistoryListener.onWatchClick(fileList.get(position));

            }
        });
        if(!LanguageHelp.isEnglish()){
            viewHolder.replay.setTextSize(12);
        }

        viewHolder.replay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ecgHistoryListener!=null)ecgHistoryListener.onReplayClick(fileList.get(position));
            }
        });
        String TIME=((fileList.get(position).getTime().replace(".","")).replace(":","")).replace(" ","");
        String TIMETWO=TIME.substring(0,TIME.length()-2);
        viewHolder.tv_FileName.setText("EK_"+TIMETWO);
        return convertView;
    }
    /*    private String getFileTime(String fileName){
          //  String milS=fileName.split(".ecg")[0];
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return  format.format(new Date(Long.valueOf(fileName)));
        }*/
    static class EcgViewHolder{
        Button tv_FileName;
        Button bt_history;
        Button bt_watch;
        Button replay;
    }
    public void clear(){
        fileList=null;
        notifyDataSetChanged();
    }
    public  interface EcgHistoryListener{
        public void onHistoryClick(HrvData fileName);
        public void onWatchClick(HrvData fileName);
        public void onReplayClick(HrvData fileName);
    }
}
